package com.example.forum.controller;

import com.example.forum.common.AppConfig;
import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("reply")
public class ArticleReplyController {

    @Autowired
    private IArticleReplyService iArticleReplyService;

    @Autowired
    private IArticleService iArticleService;

    @PostMapping("/create")
    public AppResult create(HttpServletRequest request, @NonNull Long articleId, @NonNull String content){
        HttpSession session = request.getSession(false);
        User user  = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 判断是否被禁⾔
        if (user.getState() != 0) {
            // ⽇志
            log.warn(ResultCode.FAILED_USER_BANNED.toString() + ", userId = "
                    + user.getId());
            // 返回错误信息
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 获取帖⼦信息
        Article article = iArticleService.selectById(articleId);

        // 校验帖⼦信息
        if (article == null) {
            log.warn("修改的帖⼦不存在. articleId = " + articleId);
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if (article.getState() != 0 || article.getDeleteState() != 0) {
            log.warn("帖⼦状态异常. articleId = " + articleId + ", state = " +
                    article.getState() + ", delete state = " + article.getDeleteState());
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }


        // 构造数据
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);

        // 调⽤业务层
        iArticleReplyService.create(articleReply);
        // 返回结果
        return AppResult.success();
    }

    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>>getRepliesByArticleId(@NonNull Long articleId){

        //查询帖子是否存在
        Article article = iArticleService.selectById(articleId);
        if(article==null||article.getState()==1||article.getDeleteState()==1){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //查询结果
        List<ArticleReply> articleReplies = iArticleReplyService.selectByArticleId(articleId);

        return AppResult.success(articleReplies);


    }

}

